﻿using M.Warehouse.API.Application.Command.WarehousesCommand;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.WarehousesHandlers
{
    /// <summary>
    /// 批量删除中介者
    /// </summary>
    public class WarehousesBatchDelCommandHandlers : IRequestHandler<WarehousesBatchDelCommand, int>
    {
        /// <summary>
        /// 仓库仓储接口
        /// </summary>
        private readonly IWarehouseWRepository _warehouseWRepository;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="_warehouseWRepository"></param>
        public WarehousesBatchDelCommandHandlers(IWarehouseWRepository _warehouseWRepository)
        {
            _warehouseWRepository = _warehouseWRepository;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(WarehousesBatchDelCommand request, CancellationToken cancellationToken)
        {
            var list = await _warehouseWRepository.GetAllAsync();
            var plist = list.Where(x => request.WarehouseId.Contains(x.WarehouseId.ToString())).ToList();
            foreach (var x in plist)
            {
                x.IsDeleted = true;
            }
            return await _warehouseWRepository.UpdateRange(plist);
        }
    }
}
